package com.example.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.Reservation;
import com.example.mapper.ActivityDao;
import com.example.domain.Activity;
import com.example.mapper.ReservationMapper;
import com.example.service.IActivityService;
import com.example.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ActivityImpl extends ServiceImpl<ActivityDao, Activity> implements IActivityService {

    @Autowired
    private ActivityDao activityDao;
    @Autowired
    private ReservationMapper reservationMapper;
    @Override
    public List getKeyWord(String name) {
        LambdaQueryWrapper<Activity> lwq = new LambdaQueryWrapper<>();
        lwq.like(Activity::getTitle,name).or().like(Activity::getDetail,name).or().like(Activity::getCategory,name).or().like(Activity::getActivityId,name).or().like(Activity::getActivityId,name);
        List<Activity> activities = activityDao.selectList(lwq);
        return activities;
    }

//    分页查询活动
    @Override
    public IPage<Activity> getActivity(Integer pageCurrent, Integer pageSize) {
        IPage<Activity> page = new Page<>(pageCurrent,pageSize);
        activityDao.selectPage(page,null);
        return page;
    }

    /**
     * 查询该志愿者参与了哪些活动
     * @param id
     * @param status
     * @return
     */
    List<Activity> activititylist= new ArrayList<>();
    @Override
    public List<Activity> getActivityByStatus(Integer id, Integer status) {
        LambdaQueryWrapper<Reservation> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Reservation::getStatus,status).eq(Reservation::getVolunteerId,id);
        List<Reservation> reservationList = reservationMapper.selectList(queryWrapper);
        System.out.println(reservationList);

        activititylist.clear();
        for (Reservation reservation : reservationList){
            Integer activityId = reservation.getActivityId();
            System.out.println(activityId);
            LambdaQueryWrapper<Activity> queryWrapper1=new LambdaQueryWrapper<>();
            queryWrapper1.eq(Activity::getActivityId,activityId);
            Activity activity = activityDao.selectOne(queryWrapper1);
            activititylist.add(activity);
        }
        System.out.println(activititylist);
        return activititylist;
    }
}
